local GridFrame = Grid:GetModule("GridFrame")

GridTooltip = GridFrame:NewModule("GridTooltip")

GridTooltip.defaultDB = {
}

local lastMouseOverFrame

local function FindTooltip(unit, texture, index)
    index = index or 1
    local i = 0
    local name, rank, icon, count, buffType, duration, expirationTime, source, isStealable, _, id = UnitDebuff(unit, index)
    while name or index - i > 1 do 
        if icon == texture then
            return index + i, id
        end
        i = i + 1
        name, rank, icon, count, buffType, duration, expirationTime, source, isStealable, _, id  = UnitDebuff(unit, index - i)
        if icon == texture then
            return index - i, id
        end
        name, rank, icon, count, buffType, duration, expirationTime, source, isStealable, _, id  = UnitDebuff(unit, index + i)
    end
    
    return nil
end

function GridTooltip.SetIndicator(frame, indicator, color, text, value, maxValue, texture, start, duration, stack)
	
	if texture and indicator == "icon" then
        if frame.unit and UnitExists(frame.unit)then
            frame.ToolTip = texture
            if lastMouseOverFrame then
                GridTooltip.OnEnter(lastMouseOverFrame)
            end            
        end
	end
end



function GridTooltip.ClearIndicator(frame, indicator)   
    if indicator == "icon" then
        frame.ToolTip = nil
        frame.ToolTipIndex = nil
    end
end

function GridTooltip.CreateFrames(gridFrameObj, frame)
    local f = frame
    frame:HookScript("OnEnter", GridTooltip.OnEnter)
	frame:HookScript("OnLeave", GridTooltip.OnLeave)
    --frame.IconBG.frame = frame --oh god!
end

function GridTooltip.OnEnter(frame)
    local unitid = frame.unit
    
    if not unitid then return end   
    
    lastMouseOverFrame = frame
    
    if not frame.ToolTip then return end 
    
    local id
    frame.ToolTipIndex, id = FindTooltip(unitid, frame.ToolTip, frame.ToolTipIndex)

    if frame.ToolTipIndex then
        GameTooltip_SetDefaultAnchor(GameTooltip, UIParent)
        GameTooltip:SetUnitDebuff(unitid, frame.ToolTipIndex)        
        GameTooltip:Show()
    end
end

function GridTooltip.OnLeave(iconFrame)
    GameTooltip:Hide()
    if lastMouseOverFrame == iconFrame then
        lastMouseOverFrame = nil
    end
end

function GridTooltip:OnInitialize()
   if not self.db then
		self.db = Grid.db:RegisterNamespace(self.moduleName, { profile = self.defaultDB or { } })
	end
    
    --hook the hell out of GridFrame :-)
    hooksecurefunc(GridFrame, "InitializeFrame", GridTooltip.CreateFrames)
    hooksecurefunc(GridFrame.prototype, "SetIndicator", GridTooltip.SetIndicator)
    hooksecurefunc(GridFrame.prototype, "ClearIndicator", GridTooltip.ClearIndicator)
end

function GridTooltip:OnEnable()
end

function GridTooltip:OnDisable()
end

function GridTooltip:Reset(frame)
end